import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DayTask extends StatefulWidget{

  final List stationList;
  DayTask({Key? key,required this.stationList}):super(key: key);

  _DayTaskState createState() => _DayTaskState();
}

class _DayTaskState extends State<DayTask>{
  final textStyle = TextStyle(color: Color.fromRGBO(255, 255, 255, 1.0),decoration: TextDecoration.none,fontSize: 18.sp);

  ScrollController _controller = ScrollController();

  late Timer _timer;

  double _currentPixels = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _timer = Timer.periodic(Duration(seconds: 3), (Timer t) => autoScroll());
  }

  @override
  void dispose() {
    //为了避免内存泄露，需要调用_controller.dispose
    _controller.dispose();
    _timer.cancel();
    super.dispose();
  }

  autoScroll(){
    if (_controller.position.pixels ==
        _controller.position.maxScrollExtent) {
      _currentPixels = 0;
    }else{
      _currentPixels += 300;
    }
    _controller.animateTo(
      _currentPixels,
      duration: Duration(milliseconds: 1300),
      curve: Curves.ease,
    );
  }

  Widget _buildStation(Map station){
    return AspectRatio(aspectRatio: 1.45,child: Card(
        margin: EdgeInsets.only(right: 5.w),
        clipBehavior: Clip.antiAlias,
        elevation: 1.0,
        color: Colors.transparent,
        child: Column(
          children: [
            Container(
              color: Color.fromRGBO(48,48,85, 1.0),
              child: Padding(
                padding: EdgeInsets.all(2),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(station["name"],style: TextStyle(color: Color.fromRGBO(255, 255, 255, 1),decoration: TextDecoration.none,fontSize: 22.sp),),
                  ],
                ),
              ),
            ),
            Expanded(child: Row(
              children: [
                Expanded(child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(height: 10.h,),
                    Text("计划数量: ${station['planned_quantity'].toString()}",style: textStyle,),
                    SizedBox(height: 10.h,),
                    Text("报工数: ${station['num'].toString()}",style: textStyle,),
                    SizedBox(height: 10.h,),
                    Text("上岗人数: ${station['number'].toString()}",style: textStyle,),
                  ],
                )),
                Expanded(child: Card(
                    margin: EdgeInsets.only(right: 5.w),
                    clipBehavior: Clip.antiAlias,
                    elevation: 1.0,
                    color: Color.fromRGBO(11,123,118, 0.3),
                    child:Container(
                      alignment: Alignment.center,
                      child: Text("达成率：${(station['complete']*100).toString()}%",style: textStyle),
                    ))),
              ],
            ),)
          ],
        )),);
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          color: Color.fromRGBO(48,48,85, 1.0),
          child: Padding(
            padding: EdgeInsets.all(2),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("当日生产计划任务",style: TextStyle(color: Color.fromRGBO(255, 255, 255, 1),decoration: TextDecoration.none,fontSize: 22.sp),),
              ],
            ),
          ),
        ),
        Expanded(child: Container(alignment: Alignment.centerLeft,child: SingleChildScrollView(
          controller: _controller,
          scrollDirection: Axis.horizontal,
          child: Row(
              children: widget.stationList.map((e) => _buildStation(e)).toList()
          ),
        ),)),
      ],
    );

  }
}